﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Web.Mvc;
using Xenta.Attributes;
using Xenta.Utils;
using Xenta.Web.Controllers;

namespace Xenta.Web.Areas.Wizard.Controllers
{
    public sealed class FeedbackController : ExtController
    {
        [HttpGet, RestoreState]
        public ActionResult Comment(string tgtType, int tgtID)
        {
            tgtType = StringHelper.EnsureNotNull(tgtType).Trim()
                .ToLowerInvariant();
            dynamic m = new ExpandoObject();
            m.Target = null;
            m.Type = tgtType;
            switch(tgtType)
            {
                case "blogpost":
                    m.Target = Svc.ProcessOperation("GetPublication", new
                    {
                        EntityID = tgtID
                    }, new
                    {
                        DisclosureLevel = "Base",
                        DisclosureOptions = new Dictionary<String, String>
                        {
                            { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                        }.Unload()
                    });
                    break;
                case "catalogitem":
                    m.Target = Svc.ProcessOperation("GetCatalogItem", new
                    {
                        EntityID = tgtID
                    }, new
                    {
                        DisclosureLevel = "Base",
                        DisclosureOptions = new Dictionary<String, String>
                        {
                            { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                        }.Unload()
                    });
                    break;
                case "catalogsupplier":
                    m.Target = Svc.ProcessOperation("GetCatalogSupplier", new
                    {
                        EntityID = tgtID
                    }, new
                    {
                        DisclosureLevel = "Base",
                        DisclosureOptions = new Dictionary<String, String>
                        {
                            { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                        }.Unload()
                    });
                    break;
                case "seller":
                    m.Target = Svc.ProcessOperation("GetSeller", new
                    {
                        EntityID = tgtID
                    }, new
                    {
                        DisclosureLevel = "Base"
                    });
                    break;
                default:
                    return new EmptyResult();
            }
            if(m.Target == null)
                return HttpNotFound();
            return View(m);
        }

        [HttpPost, PersistState]
        public ActionResult Comment(string tgtType, 
            int tgtID,
            string title,
            string text)
        {
            tgtType = StringHelper.EnsureNotNull(tgtType).Trim()
                .ToLowerInvariant();
            string operation = null;
            string flags = null;
            Loc ret = null;

            switch(tgtType)
            {
                case "blogpost":
                    operation = "CreatePublicationComment";
                    flags = "Active";
                    ret = Loc.Act("Show").Ctrl("Post").Area("Blog").Set(new
                    {
                        id = tgtID
                    }).Localized();
                    break;
                case "catalogitem":
                    operation = "CreateCatalogItemComment";
                    flags = "Active";
                    ret = Loc.Act("Show").Ctrl("Item").Area("Catalog").Set(new
                    {
                        id = tgtID
                    }).Localized();
                    break;
                case "catalogsupplier":
                    operation = "CreateCatalogSupplierComment";
                    flags = "Active";
                    ret = Loc.Act("Show").Ctrl("Seller").Area("Store").Set(new
                    {
                        id = tgtID
                    }).Localized();
                    break;
                case "seller":
                    operation = "CreateSellerComment";
                    flags = "Active";
                    break;
                default:
                    return new EmptyResult();
            }

            try
            {
                Svc.ProcessOperation(operation, new
                {
                    HolderID = tgtID,
                    AccountID = Svc.IsAuthenticated 
                        ? JDM.Int32(Svc.Account.EntityID) 
                        : 0,
                    Title = title,
                    Text = text,
                    Flags = flags
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return RedirectToAction(!ModelState.IsValid 
                ? Loc.Act("Comment").Set(new
                {
                    tgtType,
                    tgtID
                })
                : ret);
        }

        [HttpPost, PersistState]
        public ActionResult Rate(string tgtType,
            int tgtID,
            byte value)
        {
            tgtType = StringHelper.EnsureNotNull(tgtType).Trim()
                .ToLowerInvariant();
            string operation = null;
            string flags = null;
            Loc ret = null;

            switch(tgtType)
            {
                case "blogpost":
                    operation = "CreatePublicationRating";
                    flags = "Active";
                    ret = Loc.Act("Show").Ctrl("Post").Area("Blog").Set(new
                    {
                        id = tgtID
                    }).Localized();
                    break;
                case "catalogitem":
                    operation = "CreateCatalogItemRating";
                    flags = "Active";
                    ret = Loc.Act("Show").Ctrl("Item").Area("Catalog").Set(new
                    {
                        id = tgtID
                    }).Localized();
                    break;
                case "catalogsupplier":
                    operation = "CreateCatalogSupplierRating";
                    flags = "Active";
                    ret = Loc.Act("Show").Ctrl("Supplier").Area("Catalog").Set(new
                    {
                        id = tgtID
                    }).Localized();
                    break;
                case "seller":
                    operation = "CreateSellerRating";
                    flags = "Active";
                    ret = Loc.Act("Show").Ctrl("Seller").Area("Store").Set(new
                    {
                        id = tgtID
                    }).Localized();
                    break;
                default:
                    return new EmptyResult();
            }

            try
            {
                Svc.ProcessOperation(operation, new
                {
                    HolderID = tgtID,
                    CommentID = 0,
                    AccountID = Svc.IsAuthenticated
                        ? JDM.Int32(Svc.Account.EntityID)
                        : 0,
                    Value = value,
                    Flags = flags
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return RedirectToAction(ret);
        }
    }
}
